﻿using PMSWenLvTing.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace PMSWenLvTing.Controllers
{
    /// <summary>
    /// 所有的其他控制器继承该基类，用于判断用户是否登录
    /// </summary>
    public class BaseWLTController : Controller
    {     

        protected string hostUrl = "";
        /// <summary>
        /// Action执行前判断
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // url
            this.hostUrl = "http://" + this.Request.Url.Host;
            this.hostUrl += this.Request.Url.Port.ToString() == "80" ? "" : ":" + this.Request.Url.Port;
            this.hostUrl += this.Request.ApplicationPath;

            if (!this.checkLogin())// 判断是否登录
            {
                filterContext.Result = RedirectToRoute(new { Controller = "Login", Action = "Login" });
            }

            base.OnActionExecuting(filterContext);

        }


        /// <summary>
        /// 判断是否登录
        /// </summary>
        protected bool checkLogin()
        {
            if (this.Session["yonghuinfo"] == null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取当前系统正在使用的用户数据
        /// </summary>
        /// <returns></returns>
        public yonghu_wenlvting GetCurrentUser()
        {
            if (this.Session["yonghuinfo"] == null)
            {
                return null;
            }
            return this.Session["yonghuinfo"] as yonghu_wenlvting;
        }

        /// <summary>
        /// 获取访问客户端的IPV4地址
        /// </summary>
        /// <returns></returns>
        public string GetClientIPv4Address(HttpRequestBase Request)
        {
            string ipv4 = String.Empty;
            foreach (IPAddress ip in Dns.GetHostAddresses(GetClientIP(Request)))
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = ip.ToString();
                    break;
                }
            }
            if (ipv4 != String.Empty)
            {
                return ipv4;
            }
            // 利用 Dns.GetHostEntry 方法，由获取的 IPv6 位址反查 DNS 纪录，
            // 再逐一判断何者为 IPv4 协议，即可转为 IPv4 位址。
            foreach (IPAddress ip in Dns.GetHostEntry(GetClientIP(Request)).AddressList)
            //foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = ip.ToString();
                    break;
                }
            }

            return ipv4;
        }
        /// <summary>
        /// 获取客户端IPV4辅助方法
        /// </summary>
        /// <param name="Request"></param>
        /// <returns></returns>
        private string GetClientIP(HttpRequestBase Request)
        {
            if (null == Request.ServerVariables["HTTP_VIA"])
            {
                return Request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                return Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
        }



    }
}
